package com.studentmanagement.service;

import com.studentmanagement.dao.CourseMapper;
import com.studentmanagement.model.Course;
import com.studentmanagement.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseService {
    public Course getCourseById(Integer id) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            return mapper.getCourseById(id);
        }
    }

    public Course getCourseByCourseCode(String courseCode) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            return mapper.getCourseByCourseCode(courseCode);
        }
    }

    public List<Course> getCoursesByTeacherId(Integer teacherId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            return mapper.getCoursesByTeacherId(teacherId);
        }
    }

    public List<Course> getAllCourses() {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            return mapper.getAllCourses();
        }
    }

    public List<Course> searchCourses(String name, String department, Integer teacherId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            Map<String, Object> params = new HashMap<>();
            if (name != null && !name.trim().isEmpty()) {
                params.put("name", name);
            }
            if (department != null && !department.trim().isEmpty()) {
                params.put("department", department);
            }
            if (teacherId != null) {
                params.put("teacherId", teacherId);
            }
            return mapper.searchCourses(params);
        }
    }

    public boolean insertCourse(Course course) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            int result = mapper.insertCourse(course);
            session.commit();
            return result > 0;
        }
    }

    public boolean updateCourse(Course course) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            int result = mapper.updateCourse(course);
            session.commit();
            return result > 0;
        }
    }

    public boolean deleteCourse(Integer id) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            CourseMapper mapper = session.getMapper(CourseMapper.class);
            int result = mapper.deleteCourse(id);
            session.commit();
            return result > 0;
        }
    }
    
    /**
     * 获取课程的已选学生人数
     * @param courseId 课程ID
     * @return 已选该课程的学生人数
     */
    public int getEnrolledStudentCount(Integer courseId) {
        EnrollmentService enrollmentService = new EnrollmentService();
        return enrollmentService.getEnrolledStudentCount(courseId);
    }
} 